package edu.msse.signup.ui;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.lang.math.NumberUtils;

import edu.msse.signup.service.PositionService;
import edu.msse.signup.service.TimeSlotService;
import edu.msse.signup.service.VolunteerService;
import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Users;

public class TimeslotHandler {

	private PositionService positionService;
	private TimeSlotService timeslotService;
	private VolunteerService volunteerService;
	private Date startTime;
	private Date endTime;
	private String numberOfVolunteers;
	private ApplicationService applicationService;
	private Position currentPosition;
	private String currentPositionId;
	private String currentTimeslotId;
	private TimeSlot currentTimeslot;
	
	public TimeslotHandler() {
		
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}
	
	public String addToPosition(){
		Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		currentPositionId = (String) map.get("currentPositionId");
		
		return "newTimeslot";
	}
	
	public String createNew()
	{
		try{
			if(!applicationService.validateNumber(numberOfVolunteers))
				return null;
			
			currentPosition = getCurrentPosition();
			currentTimeslot = TimeSlotService.createTimeSlot(startTime, endTime, currentPosition, Integer.parseInt(numberOfVolunteers));
			currentPosition.addTimeSlot(currentTimeslot);
			currentPosition = PositionService.update(currentPosition);
			
		} catch (Exception x) {
            applicationService.addErrorMessage(x.getMessage());
            return null;
        }
		
		return "myEvents";
	}
	
	public String removeTimeslot(){
		Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = (String)map.get("currentTimeslotId");
		
		currentTimeslot = timeslotService.getTimeSlot(Long.parseLong(id));
		currentPosition = getCurrentPosition();
		
		List users = timeslotService.getUsers(currentTimeslot);
		Iterator i = users.iterator();
		while(i.hasNext()){
			Users user = (Users)i.next();
			volunteerService.unFillTimeSlot(user.getUserId(), currentTimeslot.getTimeSlotId());
		}
		boolean status = timeslotService.delete(currentTimeslot);
		List slots = currentPosition.getTimeSlots();
		slots.remove(currentTimeslot);
		getCurrentPosition().setTimeSlots(slots);
		PositionService.update(getCurrentPosition());
		String redirect = "myEvents";
		if (status == false){
			redirect = null;
		}
		else{
			applicationService.addInfoMessage("Timeslot for \"" + currentPosition.getName() + "\" has been removed.");		
		}
			
		
		return redirect;
	}

	public Position getCurrentPosition() {
		String pId = getCurrentPositionId();
		if (pId != null){
			int positionId = Integer.parseInt(pId);
			currentPosition = positionService.getPosition(positionId);
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentPositionId", positionId);						
		}
		
		return currentPosition;
	}


	public String getNumberOfVolunteers() {
		return numberOfVolunteers;
	}


	public void setNumberOfVolunteers(String numberOfVolunteers) {
		this.numberOfVolunteers = numberOfVolunteers;
	}

	public String modifyTimeSlots()
	{
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentPositionId", getCurrentPositionId());
	
		return "modifyTimeSlots";
	}
	
	public String getCurrentPositionId() {
		if (currentPositionId == null){
			String pId = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("currentPositionId");
			if (pId == null || "".equals(pId))
				pId = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("currentPositionId");
			currentPositionId = pId;
		}
		
		return currentPositionId;
	}


	public void setCurrentPositionId(String currentPositionId) {
		this.currentPositionId = currentPositionId;
	}


	public TimeSlotService getTimeslotService() {
		return timeslotService;
	}


	public void setTimeslotService(TimeSlotService timeslotService) {
		this.timeslotService = timeslotService;
	}


	public Date getStartTime() {
		return startTime;
	}


	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}


	public Date getEndTime() {
		return endTime;
	}
	
	public List getVolunteers(){
		return timeslotService.getUsers(getCurrentTimeslot());
	}


	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}


	public TimeSlot getCurrentTimeslot() {
		return currentTimeslot;
	}


	public void setCurrentTimeslot(TimeSlot currentTimeslot) {
		this.currentTimeslot = currentTimeslot;
	}


	public String getCurrentTimeslotId() {
		return currentTimeslotId;
	}


	public void setCurrentTimeslotId(String currentTimeslotId) {
		this.currentTimeslotId = currentTimeslotId;
	}

	public void setVolunteerService(VolunteerService volunteerService) {
		this.volunteerService = volunteerService;
	}

}
